/*
 * @Author: guoyangf
 * @Date: 2020-05-28 16:34:26
 * @LastEditTime: 2020-08-18 14:46:20
 * @Description: file content
 */
import presetVar from './presetVar';
import { tableCardTplHandle } from './tplHandle';
import { tableExtendCol } from './data-handle/tableDataHandle';
import { tableCardDataHandle } from './data-handle/tableDataHandle';
import { tplHandle } from 'ssccommon/components/auditPointBar/handle';
import { setTable } from 'ssccommon/utils/setComponentUsed';


export default async function (props) {

    /**
     * 取得模板
     */
    let createUIDomPromise = new Promise((resolve, reject) => {
        props.createUIDom(
            {},
            (tplData) => {
                resolve(tplData);

            }
        )
    })



    let [meta, multiLang] = await Promise.all([createUIDomPromise, this.sscMultiLangPromise]);

    this.tasktype = meta.context.paramMap.tasktype;
    this.queryParam.tasktype = this.tasktype;

    let tpl = meta.template;
    let button = meta.button;

    let pageConfig = await this.requestApi.getConfig({
        "tasktype": this.tasktype
    })

    pageConfig = pageConfig.data;

    let tabData = []; //tab数组
    let searchConfigArr = []; //查询区模板数组
    let listConfigArr = []; //列表区区数组
    pageConfig.tabcodes.forEach((code) => {

        let config = pageConfig[code];
        let listArea = config.listareacode;
        let searchArea = config.queryareacode;

        //列表模板字段渲染
        if (this.tableFieldRender['__FirstItem']) {
            tpl[listArea].items[0].render = this.tableFieldRender['__FirstItem'];
        }
        tpl[listArea].items.forEach((item, index) => {
            let render = this.tableFieldRender[item.attrcode];
            render && (item.render = render)


            //控制可排序的列
            if (item.attrcode === 'billno'
                || item.attrcode === 'pk_tradetype'
                || item.attrcode === 'pk_currtype'
                || item.attrcode === 'remark'
                || item.attrcode === 'amount'
                || item.attrcode === 'billdate'
                || item.attrcode === 'billmaker') {

                item.isSort = true;
            } else {

                item.isSort = false;
            }

        })
        //列表模板操作列
        tpl[listArea].items.push(tableExtendCol(multiLang)); //表格扩展列按钮
        tpl[listArea].pagination = true; //开启翻页


        //控件配置数据处理
        let specialDisplayCondition = config.specialdisplaycondition;
        let searchTpl = tpl[searchArea];
        let btnArea = config.listbuttonareacode;
        //列表控件配置
        let listConfig = {
            tabCode: code,
            isShow: false,
            regTableTplArea: listArea,
            cardFieldRender: this.cardFieldRender,
            cardTpl: null,
            rowData: null,
            cardData: {
                cardRows: [],
                listRows: []
            },
            oprBtnTplArea: code,
            optColEvent: null,
            showCardList: false,
            oprColBtnsVisibleCtrl: this.listOprColVisibleBtn[code]
        }
        listConfigArr.push(listConfig);

        //tab控件数据
        tabData.push({
            label: searchTpl.name,
            attrcode: code,
            sidebar: config.sidebar,
            isDealImage: config.isdealimage,
            isShowCount: config.isshowtotal
        });

        //查询区控件配置
        let searchConfig = this.searchTplHandle(config, code, searchArea, specialDisplayCondition, multiLang);
        specialDisplayCondition = searchConfig.specialDisplayCondition;

        let isShowAdvSearchArea = false;
        //将查询模板里的特殊展示字段不可见
        searchTpl.items.forEach((item) => {
            if (specialDisplayCondition[item.attrcode]) {
                //以下两个字段设置后才可以隐藏成功
                item.visible = false;
                item.isfixedcondition = false; //固定字段
            } else { //存在一个可见字段则显示高级查询区
                isShowAdvSearchArea || (isShowAdvSearchArea = true);
            }
        })
        //是否显示高级查询区。 
        searchConfig.isShowAdvSearchArea = isShowAdvSearchArea;

        searchConfigArr.push(searchConfig);
    })

    //默认展示第一个页签
    let defaultTabIndex = 0;
    //如通过小部件上的页签进入的，需要默认展示对应页签
    let defaultTabCode = this.props.getUrlParam("process");
    if (defaultTabCode) {
        tabData.find((tab, index) => {
            if (tab.attrcode == defaultTabCode) {
                defaultTabIndex = index;
                return true;
            }
        })
    }
    let activeTabKey = tabData[defaultTabIndex].attrcode;
    searchConfigArr[defaultTabIndex].isShow = true; //显示页签对应的搜索控件
    listConfigArr[defaultTabIndex].isShow = true; //显示页签对应的列表控件


    this.setState({
        sideBarQueryUrl: tabData[defaultTabIndex].sidebar
    })

    this.state.sideBarQueryUrl && tplHandle.call(this.auditPointBarInst, tpl);

    let hasSetonduty = button.some((btn) => {
        if (btn.key == 'Setonduty') {
            btn.visible = false;
            return true;
        }
    })

    //适配拆包
    setTable(this.props, ...listConfigArr.map(item => item.regTableTplArea))

    let initConfig = { //初始化配置
        tabData, //页签
        activeTabKey, //当前页签
        searchConfigArr, //查询控件配置
        listConfigArr, //列表控件配置
        historyTpl: tpl.ssctaskhistory, //历史记录配置
        isShowTakeOrderBtn: pageConfig.isautodispatch && hasSetonduty //是否显示接单按钮
    }

    button.forEach((btn) => {
        if (btn.area != 'detail' && !/_head$/.test(btn.area)) {
            btn.key = `${btn.key}__ssc-cap-suffix`
        }
    })
    this.beforeSetMetaAndButton(tpl, button, initConfig);
    props.meta.setMeta(tpl);
    props.button.setButtons(button);
    this.afterSetMetaAndButton(tpl, button, initConfig);


    this.setState(initConfig)


    if (this.props.sscAppType == 'sscapprove' && tabData[0].isDealImage) {
        //我的审核 判断是否安装影像和存在外系统
        let imagAndOtsResult = await this.requestApi.checkImageInstalled();
        this.isImagInstalled = imagAndOtsResult.data.isImagInstalled;
        this.alreadySettingOts = imagAndOtsResult.data.alreadySettingOts;
    }



}
